/*
 * XMLParsing.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.changelog;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Locale;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.content.res.Configuration;

import de.studiorutton.gerengdic.R;


public class XMLParsing {

	public ArrayList<Release> parse( Context ctx) throws SAXException, ParserConfigurationException, IOException {
		
		Locale l = ctx.getResources().getConfiguration().locale;
		
		InputStream in;
		if ( l.toString().startsWith( "de" ))
			in = ctx.getResources().openRawResource( R.raw.de_changelog );
		else
			in = ctx.getResources().openRawResource( R.raw.en_changelog );
		
		/* Get a SAXParser from the SAXPArserFactory. */
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();

		/* Get the XMLReader of the SAXParser we created. */
		XMLReader xr = sp.getXMLReader();

		/* Create a new ContentHandler and apply it to the XML-Reader */
		XMLDescriptionHandler xmlDescHandler = new XMLDescriptionHandler();
		xr.setContentHandler( xmlDescHandler );
		InputSource inSource = new InputSource( in );
		xr.parse( inSource );

		ArrayList<Release> itemList = xmlDescHandler.getReleasesParsed();
		return itemList;
	}
}
